/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Reader utilities Class
 *      Provides a static class for using safer BinaryReader methods with the 
 * expected Adaptive Intelligence file format.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Adaptive.Foundation.IO
{
    /// <summary>
    /// Provides a static class for using safer BinaryReader methods with the 
    /// expected Adaptive Intelligence file format.
    /// </summary>
    public static class ReaderUtils
    {
        /*--------------------------------------------------------------------------------
         * Public Shared/Static Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Shared/Static Methods / Functions
        /// <summary>
        /// Attempts to read a boolean from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The boolean value read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static bool ReadBoolean(BinaryReader reader)
        {
            bool returnValue = false;          //Return value.

            try
            {
                returnValue = reader.ReadBoolean();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read a byte value from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The byte value read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static byte ReadByte(BinaryReader reader)
        {
            byte returnValue = 0;          //Return value.

            try
            {
                returnValue = reader.ReadByte();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read a byte array from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <param name="size">
        /// The number of bytes to be read.
        /// </param>
        /// <returns>
        /// The byte array read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static byte[] ReadBytes(BinaryReader reader, int size)
        {
            byte[] returnValue = null;          //Return value.

            try
            {
                returnValue = reader.ReadBytes(size);
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read a short integer from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The short integer read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static short ReadInt16(BinaryReader reader)
        {
            short returnValue = 0;          //Return value.

            try
            {
                returnValue = reader.ReadInt16();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read an integer from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The integer read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static int ReadInt32(BinaryReader reader)
        {
            int returnValue = 0;          //Return value.

            try
            {
                returnValue = reader.ReadInt32();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read a long integer from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The long integer read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static long ReadInt64(BinaryReader reader)
        {
            long returnValue = 0;          //Return value.

            try
            {
                returnValue = reader.ReadInt64();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to read a string from an open <see cref="BinaryReader"/> instance.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="BinaryReader"/> instance to use.
        /// </param>
        /// <returns>
        /// The string read from the source.
        /// </returns>
        /// <exception cref="AiDataFormatException">
        /// Occurs when an error occurs reading the data. Any data errors are assumed to be format errors.
        /// </exception>
        public static string ReadString(BinaryReader reader)
        {
            string returnValue = null;          //Return value.

            try
            {
                returnValue = reader.ReadString();
            }
            catch (Exception)
            {
                throw new AiDataFormatException();
            }
            return returnValue;
        }
        #endregion
    }
}
